//
//  HMPictureView.swift
//  weibo28
//
//  Created by 范英强 on 16/3/26.
//  Copyright © 2016年 范英强. All rights reserved.
//

import UIKit

class HMPictureView: UICollectionView {
    
    var pictureInfo:[HMPictureInfo]?{
        didSet{
            //countLabel.text = "\(pictureInfo?.count ?? 0)"
            let size = getPictureViewSize(4)
            // 更新设置pictureview size
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
        }
    }
    
    // 获取pictureView size
    func getPictureViewSize(count: Int) -> CGSize{
        let itemMargin:CGFloat = 5
        let cellWH = (SCREENW - HMStatusCellMargin * 2 - itemMargin * 2) / 3
        // 计算多少行 多少列
        let col = count == 4 ? 2 : count >= 3 ? 3 : count
        // 比如是7张 7 -1 = 6 / 3 = 2 + 1 = 3
        let row = count == 4 ? 2 : (count - 1) / 3 + 1
        
        // 计算pictureView 宽高
        let w = CGFloat(col) * cellWH + CGFloat(col - 1) * itemMargin
        let h = CGFloat(row) * cellWH + CGFloat(row - 1) * itemMargin
        
        return CGSize(width: w, height: h)
    }

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: -  设置视图
    private func setupUI(){
        backgroundColor = RandomColor()
        // 01 添加控件
        addSubview(countLabel)
        // 02 约束控件
        countLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
    }
    
    
    // MARK: - 栏记载控件
    private lazy var  countLabel = UILabel(textColor: UIColor.redColor(), font: 30)
    
    
    

}
